package com.ruoyi.project.cms.content.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.Content;
import com.ruoyi.project.cms.content.service.ContentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cms/content")
public class ContentController extends BaseController {
    //声明业务层属性
    @Autowired
    private ContentService contentServiceImpl;

    //声明资源跳转的公共前缀
    private String prefix = "cms/content";
    //声明单元方法:完成公告类型管理页面跳转
    @RequiresPermissions("cms:content:view")
    @GetMapping()
    public String cmsType(){
        return prefix+"/type";
    }
    //声明单元方法：完成公告类型的加载
    @RequiresPermissions("cms:content:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Content content)
    {
        startPage();
        List<Content> list = contentServiceImpl.selectCmsTypeList(content);
        return getDataTable(list);
    }
    //新增单元方法:完成新增公告类型页面的转发
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 删除公告类别
     * @param ids
     * @return
     */
    @RequiresPermissions("cms:content:remove")
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(contentServiceImpl.deleteTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    /**
     * 新增保存公告类型
     */
    @RequiresPermissions("cms:content:add")
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Content content)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(contentServiceImpl.checkTypeNameUnique(content)))
        {
            return error("新增公告'" + content.getPrContent() + "'公告主题已存在");
        }
        content.setUserName(ShiroUtils.getLoginName());
        return toAjax(contentServiceImpl.insertCmsType(content));
    }
    /**
     * 修改公告，跳转修改页面并完成数据的回显
     */
    @GetMapping("/edit/{prId}")
    public String edit(@PathVariable("prId") Long prId, ModelMap mmap)
    {
        mmap.put("content", contentServiceImpl.selectTypeById(prId));
        return prefix + "/edit";
    }
    /**
     * 修改保存公告主题
     */
    @RequiresPermissions("cms:content:edit")
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Content content)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(contentServiceImpl.checkTypeNameUnique(content)))
        {
            return error("修改公告'" + content.getPrContent() + "'失败，公告主题已存在");
        }
        return toAjax(contentServiceImpl.updateCmsType(content));
    }
    /**
     * 校验岗位名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(Content content)
    {
        return contentServiceImpl.checkTypeNameUnique(content);
    }

}


